package online.foxnull2.foxmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import online.foxnull2.foxmall.product.entity.AttrAttrgroupRelationEntity;
import online.foxnull2.foxmall.product.entity.AttrEntity;
import online.foxnull2.foxmall.product.entity.AttrGroupEntity;
import online.foxnull2.foxmall.product.mapper.AttrGroupMapper;
import online.foxnull2.foxmall.product.service.AttrAttrgroupRelationService;
import online.foxnull2.foxmall.product.service.AttrGroupService;
import online.foxnull2.foxmall.product.service.AttrService;
import online.foxnull2.foxmall.product.utils.PageUtils;
import online.foxnull2.foxmall.product.utils.Query;
import online.foxnull2.foxmall.product.vo.AttrGroupRelationVo;
import online.foxnull2.foxmall.product.vo.AttrGroupWithAttrsVo;
import online.foxnull2.foxmall.product.vo.skuItem.SpuItemAttrGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    private final AttrAttrgroupRelationService attrAttrgroupRelationService;
    private AttrService attrService;

    @Autowired
    public AttrGroupServiceImpl(AttrAttrgroupRelationService attrAttrgroupRelationService) {
        this.attrAttrgroupRelationService = attrAttrgroupRelationService;
    }

    @Autowired
    public void setAttrService(AttrService attrService) {
        this.attrService = attrService;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(catelogId) && 0 != catelogId) {
            queryWrapper.eq("catelog_id", catelogId);
        }
        if (!ObjectUtils.isEmpty(params)) {
            Object key = params.get("key");
            if (!ObjectUtils.isEmpty(key)) {
                queryWrapper.and((obj) -> obj.eq("attr_group_id", key.toString()).or().like("attr_group_name", key.toString()));
            }
        }

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }


    @Override
    public void deleteRelations(List<AttrGroupRelationVo> attrGroupRelationVoList) {
        UpdateWrapper<AttrAttrgroupRelationEntity> updateWrapper = new UpdateWrapper<>();
        attrGroupRelationVoList.forEach(attrGroupRelationVo -> updateWrapper.or(attrAttrgroupRelationEntityUpdateWrapper -> attrAttrgroupRelationEntityUpdateWrapper
                .eq("attr_id", attrGroupRelationVo.getAttrId())
                .eq("attr_group_id", attrGroupRelationVo.getAttrGroupId())));
        attrAttrgroupRelationService.remove(updateWrapper);
    }

    @Override
    public List<AttrGroupWithAttrsVo> listAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        return attrGroupEntityList.stream().map(attrGroupEntity -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            attrGroupWithAttrsVo
                    .setAttrGroupId(attrGroupEntity.getAttrGroupId())
                    .setAttrGroupName(attrGroupEntity.getAttrGroupName())
                    .setSort(attrGroupEntity.getSort())
                    .setDescript(attrGroupEntity.getDescript())
                    .setCatelogId(attrGroupEntity.getCatelogId());
            List<AttrEntity> attrEntityList = attrService.getRelationAttrs(attrGroupEntity.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrEntityList);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SpuItemAttrGroup> listAttrGroupWithAttrsBySpuIdAndCatalogId(Long spuId, Long catalogId) {

        return baseMapper.listAttrGroupWithAttrsBySpuIdAndCatalogId(spuId, catalogId);
    }
}